package it.tommasocortonesi.scjp.cap2;

/*
 * Classe test per dichiarazione tipi primitivi.
 * Range e byte dei tipi primitivi:
 * 
 * TIPO		|	BYTE	|	RANGE
 * ----------------------------------
 * byte		|	1 		|	[-2^7, 2^7-1]
 * short	|	2 		|	[-2^15, 2^15-1]
 * int		|	4		|	[-2^31, 2^31-1]
 * long		|	8 		|	[-2^63, 2^63-1]
 * float	|	4 		|	na
 * double	|	8 		|	na
 * char		|	2 		|	[0, 2^16]
 */

public class PrimitivesDeclaration {
	
	public static void main(String[] args) {
		byte b1 = (byte)(maxValue(7) - 1);
		short s1 = (short)(maxValue(15) - 1);
		int i1 = (int)(maxValue(31) - 1);
		long l1 = (long)(maxValue(63) - 1);

		// valori max
		System.out.println("byte maxValue (2^7-1): " + b1);		
		System.out.println("short maxValue (2^15-1): " + s1);
		System.out.println("int maxValue (2^31-1): " + i1);
		System.out.println("long maxValue (2^63-1): " + l1);
		
		// effettuo un downcast: out of range
		i1 = 128;												
		b1 = (byte)i1;											
		System.out.println("int 128 => byte: " + b1);
		
		// arrotondamento da float a byte
		float f = 12.99f;										
		b1 = (byte)f;					
		System.out.println("float 12.99 => byte: " + b1);	
		
	}
	
	private static long maxValue(int l) {
		// metodo calcolo 2^l
		long val = 1L;
		for(int i = 0; i < l; i++) {
			val *= 2; 
		}
		return val;
	}

}
